package com.base.andlang2.ui.dashboard

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.base.andlang2.apollo.SkGqlClient
import com.base.andlang2.http.AppApi
import com.base.andlangcore.mvvm.AndLangModel
import com.base.andlangcore.http.toEmptyVersionParams
import com.base.andlangcore.http.toVersionParams
import com.base.andlangcore.util.LogUtils
import com.google.gson.JsonObject
import com.lang.rocketreserver.Query
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class DashboardViewModel(app: Application) : AndLangModel(app) {
    var data = MutableLiveData<String>().apply {
        value = "This is dashboard Fragment"
    }

    fun reqHomeData() {
        val params = HashMap<Any,Any>()
        params["type"] = 1

        makeCall<JsonObject> {
            onRequest {
                AppApi.create().reqHomeData(params.toVersionParams(app))
            }

            onSuccess {
                data.value = it.toString()
            }

            onError {
                errMsg.value = it
            }
        }
    }

    //Apollo 请求
    fun reqApolloData(){
        makeCallApollo<Query.Data> {
            onRequest {
                SkGqlClient.skQuery(
                    Query(launchId="2")
                )
            }

            onSuccess {
                data.value = it?.launch?.mission?.missionPatch
            }

            onError {
                errMsg.value = it
            }
        }
    }
}